package com.atguigu.suger.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.suger.bean.*;
import com.atguigu.suger.service.KwService;
import com.atguigu.suger.service.ProductService;
import com.atguigu.suger.service.ProvinceService;
import com.atguigu.suger.service.VisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/2/21 10:03
 */
@RestController
public class SugarController {
    
    @Autowired
    ProductService product;
    
    @RequestMapping("/gmv")
    // 如果没有传递date参数,则data会有默认值是0
    public String gmv(@RequestParam(value = "date", defaultValue = "0") int date) {
        // 如果date是0, 表示么传递日期, 就给date赋值一个当前日期
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
        /*
        {
          "status": 0,
          "msg": "",
          "data": 1201036.00323761
        }
         */
        BigDecimal gmv = product.gmv(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        result.put("data", gmv);
        return result.toJSONString();
    }
    
    @RequestMapping("/gmv/tm")
    // 如果没有传递date参数,则data会有默认值是0
    public String gmvByTm(@RequestParam(value = "date", defaultValue = "0") int date) {
        // 如果date是0, 表示么传递日期, 就给date赋值一个当前日期
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
        /*
       {
          "status": 0,
          "msg": "",
          "data": {
            "categories": [
              "苹果",
              "三星",
              "华为",
              "oppo",
              "vivo",
              "小米26"
            ],
            "series": [
              {
                "name": "手机品牌",
                "data": [
                  7136,
                  5829,
                  8980,
                  9205,
                  7410,
                  5006
                ]
              }
            ]
          }
        }
         */
        List<Map<String, Object>> list = product.gmvByTm(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (Map<String, Object> map : list) {
            Object tmName = map.get("tm_name");
            categories.add(tmName);
        }
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        JSONObject obj = new JSONObject();
        obj.put("name", "商品品牌");
        
        JSONArray data1 = new JSONArray();
        for (Map<String, Object> map : list) {
            data1.add(map.get("order_amount"));
        }
        obj.put("data", data1);
        
        series.add(obj);
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @RequestMapping("/gmv/c3")
    // 如果没有传递date参数,则data会有默认值是0
    public String gmvByC3(@RequestParam(value = "date", defaultValue = "0") int date) {
        // 如果date是0, 表示么传递日期, 就给date赋值一个当前日期
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
        /*
        {
          "status": 0,
          "msg": "",
          "data": 1201036.00323761
        }
         */
        
        List<C3> list = product.gmvByC3(date);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        
        for (C3 c3 : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", c3.getCategory3_name());
            obj.put("value", c3.getOrder_amount());
            
            data.add(obj);
        }
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @RequestMapping("/gmv/spu")
    // 如果没有传递date参数,则data会有默认值是0
    public String gmvBySpu(@RequestParam(value = "date", defaultValue = "0") int date) {
        // 如果date是0, 表示么传递日期, 就给date赋值一个当前日期
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
        /*
        {
          "status": 0,
          "msg": "",
          "data": 1201036.00323761
        }
         */
        
        List<Spu> list = product.gmvBySpu(date);
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray columns = new JSONArray();
        
        JSONObject c1 = new JSONObject();
        c1.put("name", "spu 名字");
        c1.put("id", "spu");
        columns.add(c1);
        JSONObject c2 = new JSONObject();
        c2.put("name", "销售额");
        c2.put("id", "order_amount");
        columns.add(c2);
        
        data.put("columns", columns);
        
        JSONArray rows = new JSONArray();
        
        for (Spu spu : list) {
            JSONObject row = new JSONObject();
            row.put("spu", spu.getSpu_name());
            row.put("order_amount", spu.getOrder_amount());
            
            rows.add(row);
        }
        
        data.put("rows", rows);
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @Autowired
    ProvinceService provinceService;
    
    @RequestMapping("/province")
    // 如果没有传递date参数,则data会有默认值是0
    public String province(@RequestParam(value = "date", defaultValue = "0") int date) {
        // 如果date是0, 表示么传递日期, 就给date赋值一个当前日期
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
        /*
        {
          "status": 0,
          "msg": "",
          "data": 1201036.00323761
        }
         */
        
        List<Province> list = provinceService.statsByProvince(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray mapData = new JSONArray();
        
        for (Province province : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", province.getProvince_name());
            
            obj.put("value", province.getOrder_amount());
            
            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(province.getOrder_count());
            obj.put("tooltipValues", tooltipValues);
            
            mapData.add(obj);
        }
        
        data.put("mapData", mapData);
        
        data.put("valueName", "销售额");
        
        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("订单数");
        data.put("tooltipNames", tooltipNames);
        
        JSONArray tooltipUnits = new JSONArray();
        tooltipUnits.add("个");
        data.put("tooltipUnits", tooltipUnits);
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @Autowired
    VisitorService visitorService;
    
    @RequestMapping("/visitor/hour")
    // 如果没有传递date参数,则data会有默认值是0
    public String visitorByHour(@RequestParam(value = "date", defaultValue = "0") int date) {
        // 如果date是0, 表示么传递日期, 就给date赋值一个当前日期
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
        /*
        {
          "status": 0,
          "msg": "",
          "data": 1201036.00323761
        }
         */
        
        List<Visitor> visitors = visitorService.statsByHour(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (int i = 0; i < 24; i++) {
            categories.add(i);
        }
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        // pv
        JSONObject pv = new JSONObject();
        pv.put("name", "pv");
        JSONArray pvData = new JSONArray();
        // TODO pvData中的值待实
        pv.put("data", pvData);
        series.add(pv);
        
        // uv
        JSONObject uv = new JSONObject();
        uv.put("name", "uv");
        JSONArray uvData = new JSONArray();
        // TODO uvData中的值待实
        uv.put("data", uvData);
        series.add(uv);
        
        // sv
        JSONObject sv = new JSONObject();
        sv.put("name", "sv");
        JSONArray svData = new JSONArray();
        // TODO svData中的值待实
        sv.put("data", svData);
        series.add(sv);
        
        // uj
        JSONObject uj = new JSONObject();
        uj.put("name", "uj");
        JSONArray ujData = new JSONArray();
        // TODO ujData中的值待实
        uj.put("data", ujData);
        series.add(uj);
        
        /*
        ┌─hour─┬───pv─┬──uv─┬──sv─┬─uj─┐
        │    8 │ 3294 │ 258 │ 648 │ 82 │
        │   15 │    9 │   3 │   3 │  0 │
        │   16 │  222 │  34 │  47 │  2 │
        └──────┴──────┴─────┴─────┴────┘

        构造map, key是小时, value就是对应的各个指标
         */
        Map<Integer, Visitor> hourToVisitor = new HashMap<>();
        for (Visitor visitor : visitors) {
            hourToVisitor.put(visitor.getHour(), visitor);
        }
        
        for (int hour = 0; hour < 24; hour++) {
            Visitor visitor = hourToVisitor.getOrDefault(hour, new Visitor(hour, 0L, 0L, 0L, 0L));
            
            pvData.add(visitor.getPv());
            uvData.add(visitor.getUv());
            ujData.add(visitor.getUj());
            svData.add(visitor.getSv());
        }
        
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    
    
    @Autowired
    KwService kwService;
    @RequestMapping("/kw")
    // 如果没有传递date参数,则data会有默认值是0
    public String kw(@RequestParam(value = "date", defaultValue = "0") int date) {
        // 如果date是0, 表示么传递日期, 就给date赋值一个当前日期
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
    
        List<Kw> kws = kwService.stats(date);
    
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
    
        JSONArray data = new JSONArray();
        for (Kw kw : kws) {
            JSONObject obj = new JSONObject();
    
            obj.put("name", kw.getWord());
            obj.put("value", kw.getScore());
            
    
            data.add(obj);
        }
    
        result.put("data", data);
    
        return result.toJSONString();
    }
    
}
